﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsForms;

using System.Data.SqlClient;

namespace 空气质量检测
{
    public partial class Chaxun : Form
    {

        DBHelper dBHelper = new DBHelper();
        DataSet ds = new DataSet();

        public Chaxun()
        {
            InitializeComponent();
        }

        private void Chaxun_Load(object sender, EventArgs e)
        {

            dBHelper.OpenConnection();

            string str = "select StationNamer,InputName,StartDate,EndDa,PM,Level,Notes from StationInfo inner join AirQualityInfo on StationInfo.StationID=AirQualityInfo.StationID";
            SqlDataAdapter adapter = new SqlDataAdapter(str,dBHelper.Connection);
            adapter.Fill(ds, "StationInfo");

            dBHelper.CloseConnection();


            this.dataGridView1.DataSource = ds.Tables["StationInfo"];


        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            if (this.textBox1.Text.Trim().Trim().Equals(string.Empty)) {

                MessageBox.Show("请输入查询内容！");
                return;
            }

            dBHelper.OpenConnection();

            string str = "select StationNamer, InputName, StartDate, EndDa, PM, Level, Notes from StationInfo inner join AirQualityInfo on StationInfo.StationID = AirQualityInfo.StationID where StationNamer like N'%" + this.textBox1.Text + "%'";
            SqlCommand comm = new SqlCommand(str,dBHelper.Connection);
            SqlDataReader read = comm.ExecuteReader();

            if (!read.HasRows) {
                MessageBox.Show("目前还没有该内容！");
                read.Close();
                dBHelper.CloseConnection();
                return;
            }
            read.Close();
            dBHelper.CloseConnection();


            ds.Tables["StationInfo"].Clear();
        
            dBHelper.OpenConnection();
            
            string str2= "select StationNamer, InputName, StartDate, EndDa, PM, Level, Notes from StationInfo inner join AirQualityInfo on StationInfo.StationID = AirQualityInfo.StationID where StationNamer like N'%"+this.textBox1.Text+"%'";
  
            SqlDataAdapter adapter2 = new SqlDataAdapter(str2, dBHelper.Connection);
            adapter2.Fill(ds, "StationInfo");
            this.dataGridView1.DataSource = ds.Tables["StationInfo"];
            dBHelper.CloseConnection();

        }
    }
}
